package com.hiwhitley.himei.module.meitu;

import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.hiwhitley.himei.R;
import com.hiwhitley.himei.contract.MeituContract;
import com.hiwhitley.himei.contract.model.MeituModel;
import com.hiwhitley.himei.contract.presenter.MeituPresenter;
import com.hiwhitley.himei.entity.Meitu;
import com.hiwhitley.himei.mvpframe.base.BaseFrameFragment;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.SinaRefreshView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * Created by hiwhitley on 2016/9/22.
 */

public class MeiTuFragment extends BaseFrameFragment<MeituPresenter, MeituModel> implements MeituContract.View {

    @BindView(R.id.rv_meitu)
    RecyclerView mRvMeitu;
    @BindView(R.id.srl)
    TwinklingRefreshLayout mSwipeRefreshLayout;
    @BindView(R.id.fab)
    FloatingActionButton mFab;

    private static final String TAG = "MeiTuFragment";
    private List<Meitu> mMeitus;
    private int page;
    private MultiTypeAdapter mMultiTypeAdapter;
    private List<Integer> pages = new ArrayList<>();

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_meitu;
    }

    @Override
    protected void init() {

        mMeitus = new ArrayList<>();
        page = 1;
        pages.add(page);
        mMultiTypeAdapter = new MultiTypeAdapter();
        mPresenter.loadBenefit(page);
    }

    @Override
    protected void setListener() {
        GridLayoutManager layoutManager = new GridLayoutManager(mActivity, 2);
        mRvMeitu.setLayoutManager(layoutManager);

        mMultiTypeAdapter.register(Meitu.class, new MeituViewProvider());

        mRvMeitu.setAdapter(mMultiTypeAdapter);
        mRvMeitu.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (Math.abs(dy) > 5) {
                    if (dy > 0) {
                        mFab.hide();
                    } else {
                        mFab.show();
                    }
                }
            }
        });

        mSwipeRefreshLayout.setHeaderView(new SinaRefreshView(getContext()));
        mSwipeRefreshLayout.setOverScrollBottomShow(true);
        mSwipeRefreshLayout.setAutoLoadMore(true);
        mSwipeRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                (new Handler()).postDelayed(() -> {
                    page += 1;
                    mPresenter.loadBenefit(page);
                    refreshLayout.finishLoadmore();
                }, 1200);
            }

            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                page = 1;
                mMeitus.clear();
                (new Handler()).postDelayed(() -> {
                    mPresenter.loadBenefit(page);
                    refreshLayout.finishRefreshing();
                }, 1200);
            }
        });

        mFab.setOnClickListener(v -> mRvMeitu.smoothScrollToPosition(0));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onRequestStart() {

    }

    @Override
    public void onRequestEnd() {

    }

    @Override
    public void showMeitus(List<Meitu> meitus) {
        mMeitus.addAll(meitus);
        mMultiTypeAdapter.setItems(mMeitus);
        mMultiTypeAdapter.notifyDataSetChanged();
    }
}
